package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemDescMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jt.mapper.ItemMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
	
	@Autowired
	private ItemMapper itemMapper;
	@Autowired
	private ItemDescMapper itemDescMapper;


	@Override
	@Transactional
	public void updateStatus(Long[] ids, Integer status) {
		Item item=new Item();
		item.setStatus(status);
		UpdateWrapper updateWrapper=new UpdateWrapper();
		updateWrapper.in("id", Arrays.asList(ids));
		itemMapper.update(item,updateWrapper);
	}

	@Override
	public EasyUITable findItemByPage(int page, int rows) {
		IPage<Item> iPage=new Page<>(page,rows);
		QueryWrapper queryWrapper=new QueryWrapper();
		queryWrapper.orderByDesc("updated");
//		使用接口包装分页数据
		iPage=itemMapper.selectPage(iPage, queryWrapper);

		long total=iPage.getTotal();

		List<Item> itemList=iPage.getRecords();

		return new EasyUITable(total,itemList);
	}

	@Override
	@Transactional//控制数据库实物
	public void saveItem(Item item, ItemDesc itemDesc) {
		item.setStatus(1);
		itemMapper.insert(item);

//		详情入库，MP自动回显;
		itemDesc.setItemId(item.getId());
		System.out.println("具体"+itemDesc.getItemDesc());
		System.out.println("ID编号"+itemDesc.getItemId());
		itemDescMapper.insert(itemDesc);

	}

	@Override
	@Transactional
	public void updateItem(Item item,ItemDesc itemDesc) {
		itemMapper.updateById(item);
		itemDesc.setItemId(item.getId());
		itemDescMapper.updateById(itemDesc);
	}

	@Override
	@Transactional
	public void deleteItems(Long[] ids) {

//		BaseMapper实现
//		itemMapper.deleteBatchIds(Arrays.asList(ids));
		itemMapper.deleteItems(ids);
		itemDescMapper.deleteById(ids);
	}

	@Override
	public ItemDesc findItemByDesc(Long itemId) {
		return itemDescMapper.selectById(itemId);
	}
}
